package top.fengleifeng.question;

import java.util.LinkedList;
import java.util.List;
import top.fengleifeng.util.Constants;

/**
 * @author : fengelfeing
 * @version V1.0
 * @Project: leetcode-test
 * @Package : top.fengleifeng.question
 * @Description:
 * @Date: 2020年11月05日9:29 下午
 */
public class Num448找到所有数组中消失的数字 {
//
//    class Solution {
//
//        public List<Integer> findDisappearedNumbers(int[] nums) {
//            int[] ints = new int[nums.length];
//            for (int i = 0; i < nums.length; i++) {
//                ints[nums[i] - 1] = 1;
//            }
//            List<Integer> list = new LinkedList<>();
//            for (int i = 0; i < ints.length; i++) {
//                if (ints[i] != 1) {
//                    list.add(i + 1);
//                }
//            }
//            return list;
//        }
//    }

    class Solution {
        public List<Integer> findDisappearedNumbers(int[] nums) {

            // Iterate over each of the elements in the original array
            for (int i = 0; i < nums.length; i++) {

                // Treat the value as the new index
                int newIndex = Math.abs(nums[i]) - 1;

                // Check the magnitude of value at this new index
                // If the magnitude is positive, make it negative 
                // thus indicating that the number nums[i] has 
                // appeared or has been visited.
                if (nums[newIndex] > 0) {
                    nums[newIndex] *= -1;
                }
            }

            // Response array that would contain the missing numbers
            List<Integer> result = new LinkedList<Integer>();

            // Iterate over the numbers from 1 to N and add all those
            // that have positive magnitude in the array
            for (int i = 1; i <= nums.length; i++) {

                if (nums[i - 1] > 0) {
                    result.add(i);
                }
            }

            return result;
        }
    }

    public static void main(String[] args) {
        int[] ints = {4,3,2,7,8,2,3,1};
        List<Integer> disappearedNumbers = new Num448找到所有数组中消失的数字().new Solution().findDisappearedNumbers(ints);
        Constants.pringLinkedList(disappearedNumbers);
    }
}
